;
(function() {
  Array.prototype.removeRepeatBuildingAndFLoor = function() {
    var res = [this[0]];
    for (var i = 1; i < this.length; i++) {
      var repeat = false;
      for (var j = 0; j < res.length; j++) {
        if (this[i].buildingName == res[j].buildingName && this[i].floorNum == res[j].floorNum) {
          repeat = true;
        }
      }
      if (!repeat) {
        res.push(this[i]);
      }
    }
    return res;
  };

  var app = new Vue({
    el: '#withdrawPlan',
    data: {
      contractId:null,
      hireCause: [],
      hireCauseActive: '',
      data: {
        expenses: []
      },
      contratdata: {
        st: [],
        ot: []
      },
      billsno: null,
      billsId: null,
      zid: 0,
      finish: [],
      finishList: [],
      todoList: [],
      jsonArr: {},
      matterActive: [],
      st:[],    // 车位
      wp:[],    // 工位
      stTotalNum:0,    // 车位总数
      wpTotalNum:0,     // 工位总数
      totalArea:0,   // 单元总面积
      building:[],    // 楼宇信息
      projectName:null, // 项目名
      unitOpenFlag:true,
      wpOpenFlag:true,
      stOpenFlag:true,
      unitId:null,      // 单元id
      unitDetailsFlag:false,
      quanxian:window.localStorage.getItem('permissions'),
    },
    filters: {
      formatDate: function(time) {
        if (time === undefined) {
          return '--'
        }
        var date = new Date(time)
        return hlDateUtil.formatUTCDate(date, 'yyyy-MM-dd')
      },
      formatNum:function(item){
        if(item == '--')return'--'
        return hlNumberUtil.formatNum(item)
      },
      auditState: function(state) {
        return hlCodeConvert.withdrawAuditState(state)
      }
    },
    methods: {
      // 获取合同单元信息
      getContractUnitsData:function(){
        var self = this;
        hlhttp.get({
          url: 'contract/units',
          data: {id:self.contractId},
          success: function (data) {
            // 单元
            var buildingData = self.dealWithUnitData(data)
            self.totalArea = buildingData.totalArea
            self.building = buildingData.building
            // 车位
            self.st = [];
            self.wp = [];
            var stTotalNum = 0;
            var wpTotalNum = 0;
            data.map(function(item) {
              if (item.positions && item.unitType === 'ST') {
                stTotalNum = stTotalNum + item.positions.length;
                item['num'] = item.positions.length;
                self.st.push(item)
              }
              if (item.positions && item.unitType === 'WP') {
                wpTotalNum = wpTotalNum + item.positions.length;
                item['num'] = item.positions.length;
                self.wp.push(item)
              }
            })
            self.stTotalNum = stTotalNum;
            self.wpTotalNum = wpTotalNum;
          }
        })
      },
      // 单元数据去重整合处理
      dealWithUnitData:function(unitsData){
        // 去除含有工位车位的单元
        var units = [];
        unitsData.map(function(_item){
          if(_item.positions.length === 0){
            units.push(_item)
          }
        })
        if(units.length === 0){
          return {
            building:[],
            totalArea:0
          }
        }
        // 合并单元项
        var unit = [];
        var totalArea = 0;
        for (var i = 0; i <units.length; i++) {
            unit.push({
              buildingName: units[i].buildingName,
              floorNum: units[i].floorNum,
              floorName: units[i].floorName,
              unit: [],
              unitId:[]
            })
        }
        var unitArr = unit.removeRepeatBuildingAndFLoor()
        for (var z = 0; z < unitArr.length; z++) {
          for (var m = 0; m < units.length; m++) {
            if (units[m].buildingName == unitArr[z].buildingName && units[m].floorNum == unitArr[z].floorNum) {
              unitArr[z].unit.push(units[m].unitName+'('+ hlNumberUtil.formatNum(units[m].unitAcreage) +'m²)')
              unitArr[z].unitId.push(units[m].unitId)
              totalArea = totalArea + Number(units[m].unitAcreage)
            }
          }
        }
        // 合并楼层项
        var building = [];
        for (var j = 0; j< unitArr.length; j++){
          building.push({
            buildingName: unitArr[j].buildingName,
            floor:[]
          })
        }
        var buildingArr = hlArrayUtil.removeRepeat(building,'buildingName')
        for (var k = 0; k < buildingArr.length; k++) {
          for (var n = 0; n < unitArr.length; n++) {
            if (unitArr[n].buildingName == buildingArr[k].buildingName) {
              buildingArr[k].floor.push({
                'floorNum':unitArr[n].floorNum,
                'floorName':unitArr[n].floorName,
                'unit':unitArr[n].unit,
                'unitId':unitArr[n].unitId
              })
            }
          }
        }
        return {
          building:buildingArr,
          totalArea:totalArea
        }
      },
      // 获取单元详情
      getUnitDetails:function(unitId){
        var self = this;
        self.unitId = unitId;
        self.unitDetailsFlag = true;
      },
      closeUnitPop:function(state){
        this.unitId = null;
        this.unitDetailsFlag = false;
      },
      // 查看出租资源详情信息
      checkDetailMes:function(type){
        var self = this;
        if(type === 'unit'){
          if(self.unitOpenFlag === true){
            self.unitOpenFlag = false
            return false
          }
          if(self.unitOpenFlag === false){
            self.unitOpenFlag = true
            return false
          }
        }
        if(type === 'wp'){
          if(self.wpOpenFlag === true){
            self.wpOpenFlag = false;
            return false;
          }
          if(self.wpOpenFlag === false){
            self.wpOpenFlag = true
            return false;
          }
        }
        if(type === 'st'){
          if(self.stOpenFlag === true){
            self.stOpenFlag = false
            return false;
          }
          if(self.stOpenFlag === false){
            self.stOpenFlag = true
            return false;
          }
        }
      },
      getData: function() {
        var _this = this;
        var id = hlUrlUtil.GetQueryString('id')
        this.zid = id
        hlhttp.get({
          url: 'withdraw,expenses',
          data: {
            id: id
          },
          success: function(res) {
            _this.data = res
            _this.contractId = res.contractId
            _this.hireCauseActive = res.reason
            _this.todoList = res.todoList
            if (res.finishList) {
              _this.finishList = _this.todoList.concat(res.finishList)
              _this.matterActive = res.finishList
            } else {
              _this.finishList = _this.todoList
            }

            function arrCheck(arr){
              var newArr = [];
              for(var i=0;i<arr.length;i++){
                var temp=arr[i];
                var count=0;
                for(var j=0;j<arr.length;j++){
                  if(arr[j]==temp){
                    count++;
                    arr[j]=-1;
                  }
                }
                if(temp != -1){
                  newArr.push(temp+":"+count)
                }
              }
              return newArr;
            }

            if (_this.finishList) {
              _this.jsonArr = arrCheck(_this.finishList)
            }

            loginPrem.loginList()
            if (res.auditState == 'WJS') {
              _this.getbills(res.contractId)
            }
            contract.formatUnitData(res.contractId, function(data) {
              _this.contratdata = data
            })
            _this.getContractUnitsData()
          }
        })
      },
      //获取待结算
      getbills: function (id) {
        var _this = this;
        hlhttp.get({
          url: 'contracts/bills',
          data: {
            contract_id: id
          },
          success: function (res) {
            for (var i = 0; i < res.length; i++) {
              if (res[i].source == 'T') {
                _this.billsno = res[i].no;
                _this.billsId = res[i].id;
              }
            }
          }
        })
      },
      //更新进度
      gxProgress: function() {
        var id = this.zid;
        var _this = this;
        hlhttp.put({
          url: 'withdraw/progress',
          data: {
            id: id,
            reason: _this.hireCauseActive,
            finish_list: _this.matterActive.toString() || '#'
          },
          success: function() {
            hl.toast('进度已更新')
            _this.getData()
          }
        })
      },
      //获取退租原因
      withdrawReasonsFn: function() {
        var _this = this;
        hlhttp.get({
          url: 'withdraw_reasons',
          success: function(res) {
            _this.hireCause = res
          }
        })
      },
      //获取待办事项
      getFinsh: function(item, i, event) {
        var that = event.currentTarget;
        if (that.className.indexOf('active') > -1) {
          that.className = ''
        } else {
          that.className = 'active'
        }
      },
      //大图
      bigPicShow: function(arr, i) {
        $.imageShow(arr, i)
      },
      //提交结算
      wjc: function () {
        var id = this.zid;
        var _this = this;
        hlhttp.put({
          url: 'withdraw/wjs',
          data: {
            id: id
          },
          success: function() {
            hl.toast('提交结算成功')
            window.location.href = window.location.protocol+'//' + window.location.host + window.location.pathname + '#/withdrawAudit?id='+_this.zid
          }
        })
      }
    },
    mounted: function() {
      // this.getContractUnitsData()
      this.getData()
      this.withdrawReasonsFn()
    }
  })

}())
